<?php
/**
 * Description of addProject
 *
 * @author Runner
 */

class addProjectInfo extends AddInformation{
    private $project;
    private $queryStr;
    
    function __construct($project=null) {
        $this->project = $project;
        $this->queryStr = sprintf('insert into Project(name, source, outlay, firstResponsible, secondResponsible,'
                                        . ' thirdResponsible, participant, startTime, endTime, finishTime, state, auditingState)'.
                                        'values("%s", "%s", %f, "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")', 
                                        $this->project->name, $this->project->source,
                                        $this->project->outlay, $this->project->firstResponsible,
                                        $this->project->secondResponsible, $this->project->thirdResponsible,
                                        $this->project->participant, $this->project->startTime,
                                        $this->project->endTime, $this->project->finishTime,
                                        $this->project->state, $this->project->auditingState
                                    );
        
    }
    
    public function add($linkIdentify) {
        if($this->check()){
            if($this->addInfo($this->queryStr, $linkIdentify)){
                return true;
            }
            return false;
        }
    }
    
    /* 检查数据是否合格 */
    function check(){
        if($this->project==NULL){
            return false;
        }
        /* 检查是否为空字符串 */
        foreach (AttributesName::$project as $value) {
            if($this->project->$value==""){
                return false;
            }
        }
        /* 检查是否符合字符串要求 */
        $timePattern = '/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/';       // 日期的正则表达式模式
        if( preg_match($timePattern, $this->project->startTime)==0 ){        // 开始时间
            return false;
        }
        if( preg_match($timePattern, $this->project->endTime)==0 ){     // 结束时间
            return false;
        }
        if( preg_match($timePattern, $this->project->finishTime)==0 ){     // 结题时间
            return false;
        }
        /* 比较日期 */
        if( strtotime($this->project->startTime)>strtotime($this->project->endTime) 
            || strtotime($this->project->startTime)>strtotime($this->project->finishTime) 
            || strtotime($this->project->endTime)>strtotime($this->project->finishTime) )
        {
            return false;
        }
        return true;
    }
}
